package com.mohe.dangjian.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mohe.dangjian.dao.IDao;
import com.mohe.dangjian.model.Role;
import com.mohe.dangjian.service.interfaces.IRole;
import com.mohe.dangjian.utils.Page;

public class RoleImpl implements IRole {
	
	private IDao<Role> roleDao;
	
	public IDao<Role> getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(IDao<Role> roleDao) {
		this.roleDao = roleDao;
	}

	@Override
	public List<Role> getRoleList(Role role, Page page) {
		StringBuffer where = new StringBuffer();
		where.append("from Role where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(role.getName())) {
			where.append(" and name like ?");
			params.add("%%" + role.getName() + "%%");
		}
		// 管理员权限
		if (role.getAdminPower() != null) {
			where.append(" and content = ?");
			params.add(role.getAdminPower());
		}
		// 一级权限
		if (role.getOneLevelPower() != null) {
			where.append(" and oneLevelPower = ?");
			params.add(role.getOneLevelPower());
		}
		// 二级权限
		if (role.getTwoLevelPower() != null) {
			where.append(" and twoLevelPower = ?");
			params.add(role.getTwoLevelPower());
		}
		// 一级权限编号
		if (!StringUtils.isBlank(role.getOneLevelNO())) {
			where.append(" and oneLevelNO = ?");
			params.add(role.getOneLevelNO());
		}
		// 二级权限编号
		if (!StringUtils.isBlank(role.getTwoLevelNO())) {
			where.append(" and twoLevelNO = ?");
			params.add(role.getTwoLevelNO());
		}

		where.append(" order by createDate desc");
		List<Role> list = roleDao.query(where.toString(),
				(page.getCurrPage() - 1) * page.getPageSize(),
				page.getPageSize(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

	@Override
	public Integer getCount(Role role) {
		StringBuffer where = new StringBuffer();
		where.append("from Role where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(role.getName())) {
			where.append(" and name like ?");
			params.add("%%" + role.getName() + "%%");
		}
		// 管理员权限
		if (role.getAdminPower() != null) {
			where.append(" and content = ?");
			params.add(role.getAdminPower());
		}
		// 一级权限
		if (role.getOneLevelPower() != null) {
			where.append(" and oneLevelPower = ?");
			params.add(role.getOneLevelPower());
		}
		// 二级权限
		if (role.getTwoLevelPower() != null) {
			where.append(" and twoLevelPower = ?");
			params.add(role.getTwoLevelPower());
		}
		// 一级权限编号
		if (!StringUtils.isBlank(role.getOneLevelNO())) {
			where.append(" and oneLevelNO = ?");
			params.add(role.getOneLevelNO());
		}
		// 二级权限编号
		if (!StringUtils.isBlank(role.getTwoLevelNO())) {
			where.append(" and twoLevelNO = ?");
			params.add(role.getTwoLevelNO());
		}

		return roleDao.getCount(where.toString(), params);
	}

	@Override
	public boolean saveRole(Role role) {
		return roleDao.insert(role);
	}

	@Override
	public boolean saveRoleList(List<Role> roleList) {
		return roleDao.insert(roleList);
	}

	@Override
	public Role getRoleById(Long timeKey) {
		return roleDao.getByKey(timeKey);
	}

	@Override
	public boolean updateRole(Role role) {
		return roleDao.update(role);
	}

	@Override
	public boolean delRole(Long timeKey) {
		return roleDao.delete(timeKey);
	}

	@Override
	public List<Role> getAllRole() {
		StringBuffer where = new StringBuffer();
		where.append("where timeKey > -1 order by createDate desc");
		return roleDao.query(where.toString(), null);
	}

	@Override
	public List<Role> getRoleList(Role role) {
		StringBuffer where = new StringBuffer();
		where.append("from Role where 1=1");
		List<Object> params = new ArrayList<Object>();
		// 名称
		if (!StringUtils.isBlank(role.getName())) {
			where.append(" and name like ?");
			params.add("%%" + role.getName() + "%%");
		}
		// 管理员权限
		if (role.getAdminPower() != null) {
			where.append(" and content = ?");
			params.add(role.getAdminPower());
		}
		// 一级权限
		if (role.getOneLevelPower() != null) {
			where.append(" and oneLevelPower = ?");
			params.add(role.getOneLevelPower());
		}
		// 二级权限
		if (role.getTwoLevelPower() != null) {
			where.append(" and twoLevelPower = ?");
			params.add(role.getTwoLevelPower());
		}
		// 一级权限编号
		if (!StringUtils.isBlank(role.getOneLevelNO())) {
			where.append(" and oneLevelNO = ?");
			params.add(role.getOneLevelNO());
		}
		// 二级权限编号
		if (!StringUtils.isBlank(role.getTwoLevelNO())) {
			where.append(" and twoLevelNO = ?");
			params.add(role.getTwoLevelNO());
		}

		where.append(" order by createDate desc");
		List<Role> list = roleDao.query(where.toString(), params);
		if (list != null && list.size() > 0) {
			return list;
		}
		return null;
	}

}
